﻿/*
   Copyright 2012 University of Southampton

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
*/

using System;
using System.ComponentModel;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
using System.Data;
using uk.ac.soton.ses.WebControls;

namespace uk.ac.soton.ses.ExperimentParameterEntryWebPart
{
    [ToolboxItemAttribute(false)]
    public class ExperimentParameterEntryWebPart : WebPart
    {
        // Visual Studio might automatically update this path when you change the Visual Web Part project item.
        //private const string _ascxPath = @"~/_CONTROLTEMPLATES/uk.ac.soton.ses/ExperimentParameterEntryWebPart/ExperimentParameterEntryWebPartUserControl.ascx";
        //private ExperimentParameterEntryWebPartUserControl _experimentParameterEntryControl=null;
        private IHDCWebPartSelectedExperimentID _experimentProvider=null;
        private HyperLink _hyperlink = null;
        private Panel _panel = null;

        //IWebPartRow _provider;
        //private DataRowView _dataRowView;
        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);

            //Always load the control so we can process postbacks but set it as invisible in case the provider tells us we have no experiment to bind to.
            //We'll make it visible again in OnPreRender as that is the only stage we'll definitely have a provider.
            //this._experimentParameterEntryControl = (ExperimentParameterEntryWebPartUserControl)Page.LoadControl(_ascxPath);
            //this._experimentParameterEntryControl.Visible = false;
            //this.Controls.Add(this._experimentParameterEntryControl);

            this._panel = new Panel();
            this._panel.Visible = false;
            this.Controls.Add(this._panel);

            Label lbl_notSupported = new Label();
            lbl_notSupported.Text = "Parameter entry not supported in a web part.";
            this._panel.Controls.Add(lbl_notSupported);

            this._hyperlink = new HyperLink();
            this._hyperlink.Text = "Use experiment details page instead.";
            this._hyperlink.Target = "_blank";
            this._panel.Controls.Add(this._hyperlink);

        }
        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);
        }
        protected override void CreateChildControls()
        {
            base.CreateChildControls();
        }
        protected override void Render(HtmlTextWriter writer)
        {
            base.Render(writer);
        }
        protected override void OnPreRender(EventArgs e)
        {
            base.OnPreRender(e);

            if (this._experimentProvider == null)
            {
                DisplayMessage("No connection. Please connect me to an Experiment list.");
            }
            else
            {
                int experimentID = this._experimentProvider.GetSelectedExperimentID();
                if (experimentID<=0)
                {
                    DisplayMessage("Please select an experiment.");
                }
                else
                {
                    //this._experimentParameterEntryControl.Visible = true;
                    //this._experimentParameterEntryControl.BindToExperiment(experimentID);
                    this._panel.Visible = true;
                    this._hyperlink.NavigateUrl = HDCConfiguration.GetUrl_ExperimentDetails(experimentID);
                }
            }
        }

        [ConnectionConsumer("Selected experiment", "SelectedExperiment")]
        public void SetExperimentConnectionInterface(IHDCWebPartSelectedExperimentID provider)
        {
            this._experimentProvider = provider;
        }

        /*
        [ConnectionConsumer("Row")]
        public void SetConnectionInterface(IWebPartRow provider)
        {
            this._provider = provider;
            if (_provider == null)
            {
                DisplayMessage("No connection. Please connect me to an Experiment Data Files list.");
            }
            else
            {
                _provider.GetRowData(new RowCallback(GetRowDataCallback));
            }
        }
        public void GetRowDataCallback(object rowData)
        {
            try
            {
                _dataRowView = (DataRowView)rowData;
            }
            catch (Exception e)
            {
                _dataRowView = null;
                DisplayMessage(String.Format("Exception {0}", e.Message));
            }
        }
        */

        private void DisplayMessage(string error)
        {
            Label messageLabel = new Label();
            Controls.Add(messageLabel);
            messageLabel.Text = error;
        }
    }
}
